/*
The MIT License (MIT)

Copyright (c) 2013 Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "hhdFile.h"

#include <stdio.h>

namespace hhd
{
	bool fileOpen( const char8_t* in_name, uint32_t in_flags, FileHandle& out_file_handle )
	{
		const char* open_flags = "rb";

		switch(in_flags)
		{
		case File::Binary | File::Read:
		case File::Read:
		case File::Binary:
			open_flags = "rb";
			break;
			
		case File::Read | File::Text:
		case File::Text:
			open_flags = "rt";
			break;

		case File::Binary | File::Write:
		case File::Write:
			open_flags = "wb";
			break;

		case File::Write | File::Text:
			open_flags = "wt";
			break;
		}

		out_file_handle = fopen(in_name, open_flags);
		return out_file_handle ? true : false;
	}

	bool fileWrite( FileHandle in_handle, const char8_t* in_str )
	{
		int result = fputs(in_str, in_handle);
		return result == EOF ? false : true;
	}

	bool fileWriteN( FileHandle in_handle, const char8_t* in_str, size_t in_count )
	{
		size_t size = fwrite(in_str, sizeof(char8_t), in_count, in_handle);
		return size == in_count ? true : false;
	}

	void fileReset( FileHandle in_handle )
	{
		rewind(in_handle);
	}

	bool fileFlush( FileHandle in_handle )
	{
		int result = fflush(in_handle);
		return result == EOF ? false : true;
	}

	void fileClose( FileHandle in_handle )
	{
		if(in_handle)
		{
			fflush(in_handle);
			fclose(in_handle);
		}
	}

	bool fileRead( FileHandle in_handle, char8_t& out_char )
	{
		int result = fgetc(in_handle);
		if (result != EOF)
		{
			out_char = (char8_t)result;
			return true;
		}
		else
		{
			return false;
		}
	}

	bool fileReadN( FileHandle in_handle, char8_t* inout_buffer, size_t in_size )
	{
		size_t result = fread(inout_buffer, sizeof(char8_t), in_size, in_handle);
		return result == in_size ? true : false;
	}

	bool fileSeek( FileHandle in_handle, size_t in_bytes )
	{
		int result = fseek(in_handle, in_bytes, SEEK_CUR);
		return result == 0 ? true : false;
	}

	bool fileSize( const char8_t* in_filename, uint64_t& out_size )
	{
		WIN32_FILE_ATTRIBUTE_DATA file_attributes;
		BOOL result = GetFileAttributesExA(in_filename, GetFileExInfoStandard, &file_attributes);
		if(result != 0)
		{
			out_size =  (static_cast<uint64_t>(file_attributes.nFileSizeHigh) << 32) | file_attributes.nFileSizeLow;
		}
		return result != 0 ? true : false;
	}

}